<?php

namespace Console\Controller;

/**
 * 信鸽控制器
 */
class SettingController extends AuthController {

    private $settingLogic;

    public function _initialize() {
        parent::_initialize();
        $this->settingLogic = A('Console/Setting', 'Logic');
    }

    /**
     * 设置主页
     */
    public function index() {
        $sysInfo = $this->settingLogic->getSysInfo();

        $this->assign("sysInfo", $sysInfo);
        $this->display();
    }

    /**
     * 获取眼砂列表
     */
    public function doGetEyeList() {
        $eyeDic = $this->settingLogic->getEyeDic();
        $this->assign("eyeDic", $eyeDic);
        $html = $this->fetch("doGetEyeList");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 添加燕砂
     */
    public function doAddEye() {
        $type = I('post.type');
        if (empty($type)) {
            $this->ajaxReturn(static::$res->err("请输入眼砂"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->settingLogic->addEye($userId, $type);
        $this->ajaxReturn($res);
    }

    /**
     * 编辑眼砂
     */
    public function doEditEye() {
        $id = I('post.id');
        $type = I('post.type');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("编辑ID不能为空"));
        }
        if (empty($type)) {
            $this->ajaxReturn(static::$res->err("请输入眼砂"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->settingLogic->editEye($userId, $id, $type);
        $this->ajaxReturn($res);
    }

    /**
     * 删除眼砂
     */
    public function doDelEye() {
        $id = I('post.id');

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("编辑ID不能为空"));
        }

        $res = $this->settingLogic->delEye($id);
        $this->ajaxReturn($res);
    }

    /**
     * 获取眼砂列表
     */
    public function doGetColorList() {
        $colorDic = $this->settingLogic->getColorDic();
        $this->assign("colorDic", $colorDic);
        $html = $this->fetch("doGetColorList");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 添加颜色
     */
    public function doAddColor() {
        $type = I('post.type');
        if (empty($type)) {
            $this->ajaxReturn(static::$res->err("请输入颜色"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->settingLogic->addColor($userId, $type);
        $this->ajaxReturn($res);
    }

    /**
     * 编辑颜色
     */
    public function doEditColor() {
        $id = I('post.id');
        $type = I('post.type');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("编辑ID不能为空"));
        }
        if (empty($type)) {
            $this->ajaxReturn(static::$res->err("请输入颜色"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->settingLogic->editColor($userId, $id, $type);
        $this->ajaxReturn($res);
    }

    /**
     * 删除颜色
     */
    public function doDelColor() {
        $id = I('post.id');

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("编辑ID不能为空"));
        }

        $res = $this->settingLogic->delColor($id);
        $this->ajaxReturn($res);
    }

    /**
     * 编辑设置系统
     */
    public function doEditSysInfo() {
        $data = I("post.");
        $res = $this->settingLogic->editSysInfo($data);
        $this->ajaxReturn($res);
    }

    /**
     * 备份数据库
     */
    public function doBackups() {
        $backupsLogic = A('Console/Backups', 'Logic');
        $fileName = $backupsLogic->toSql();
        $this->ajaxReturn(static::$res->suc("成功", $fileName));
    }

}
